﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Instrument
{
    public class Production
    {
        public string Model { get; set; }//型号
        public Quality ProductionQuality = Quality.Unqualified;//合格状态,默认不合格
        /// <summary>
        /// 合格电阻值
        /// </summary>
        public double QualifiedResistance { get; set; }
        public double AllowOffset { get; set; }//单通道阻值误差范围内允许的偏移量
        public double Range { get; set; }//单片应变计允许的极差值差
        /// <summary>
        /// 通道数量
        /// </summary>
        public int ResistanceCount { get; set; }
        private double[] resistanceValue;
        public double[] ResistanceValue//各通道电阻值
        {
            get
            {
                return resistanceValue;
            }
            set
            {
                //if (value.Length == ResistanceCount) //检查通道数
                //{
                    resistanceValue = value;
                //}
                //else
                //    throw new Exception("通道数有误");
            }
        }
        /// <summary>
        /// 将一组电阻值写入当前实例
        /// </summary>
        /// <param name="Row"></param>
        public void SetResistanceValue(double[] Row)
        {
            ResistanceValue = Row;
            ProductionQuality = Analyze(Row);
        }
        private Quality Analyze(double[] Row)
        {
            double Max = Row[0];
            double Min = Row[0];
            foreach(var item in Row)
            {
                if( item - QualifiedResistance > AllowOffset && item - QualifiedResistance < 0 || item <= -1)
                {
                    return Quality.Unqualified;
                }
                if(item > Max)
                {
                    Max = item;
                }
                else if(item <= Min)
                {
                    Min = item;
                }
            }
            if((Max - Min) > Range)
            {
                return Quality.Unqualified;
            }
            else
            {
                return Quality.Qualified;
            }
        }
    }
}
